package com.sodimac.util.commons
{
	import mx.rpc.soap.SOAPHeader;
	import com.adobe.crypto.WSSEUsernameToken;
	

	public class SOAPHeaderUtil
	{

		private static const WSSE_SECURITY:QName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security" );
		
		public static function returnWSSEHeaderWithNonceAndTimestamp(username:String, password:String, nonce:String=null, timestamp:Date=null):SOAPHeader
		{
			var userToken:String = "UsernameToken-"+Math.round(Math.random()*999999).toString();
			var wsseToken:Array = WSSEUsernameToken.getUsernameTokenAsArray(username, password, nonce, timestamp);
			var headerXML : XML =  <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         			<wsse:UsernameToken wsu:Id={userToken} xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>
	            		<wsse:Username>{wsseToken[0]}</wsse:Username>
	            		<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">{password}</wsse:Password>
	            		<wsse:Nonce>{wsseToken[2]}</wsse:Nonce>
	            		<wsu:Created>{wsseToken[3]}</wsu:Created>
         			</wsse:UsernameToken>
      			</wsse:Security>;
      		var header:SOAPHeader = new SOAPHeader( WSSE_SECURITY, headerXML );
      		return header;
		}
		
		public static function returnWSSEHeaderWithoutNonceAndTimestamp(username:String, password:String):SOAPHeader
		{
			var userToken:String = "UsernameToken-"+Math.round(Math.random()*999999).toString();
			var wsseToken:Array = WSSEUsernameToken.getUsernameTokenAsArray(username, password);
			var headerXML : XML =  <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         			<wsse:UsernameToken wsu:Id={userToken} xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>
	            		<wsse:Username>{wsseToken[0]}</wsse:Username>
	            		<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">{password}</wsse:Password>
         			</wsse:UsernameToken>
      			</wsse:Security>;
      			
      		var header : SOAPHeader = new SOAPHeader( WSSE_SECURITY, headerXML );
      		return header;
		}

	}
}


